package tech.summerly.weather.component.fragments

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_weather_tower.view.*
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import tech.summerly.weather.R
import tech.summerly.weather.component.adapter.WeatherTowerAdapter
import tech.summerly.weather.data.WeatherRepository
import tech.summerly.weather.model.Region

class WeatherTowerFragment : Fragment() {

    companion object {
        const val TAG = "WeatherTowerFragment"
    }

    private val adapter by lazy { WeatherTowerAdapter(this.activity!!) }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_weather_tower, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val recycler = view.recycler
        recycler.adapter = adapter
    }

    fun setRegion(region: Region) = launch(UI) {
        val realTime = WeatherRepository.getRealtimeWeather(region)
        val forecast = WeatherRepository.getForecastWeather(region)
        adapter.setWeather(realTime, forecast, region)
    }

}